home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Extensions / Imaging / PIL / GbrImagePlugin.py < prev    next >
Encoding:
Text File  |  2000-06-23  |  1.5 KB  |  68 lines

  1. #
  2. # The Python Imaging Library
  3. # $Id: GbrImagePlugin.py,v 1.1.1.2 1999/01/13 09:39:45 sjoerd Exp $
  4. #
  5. # load a GIMP brush file
  6. #
  7. # History:
  8. #    96-03-14 fl    Created
  9. #
  10. # Copyright (c) Secret Labs AB 1997.
  11. # Copyright (c) Fredrik Lundh 1996.
  12. #
  13. # See the README file for information on usage and redistribution.
  14. #
  15.  
  16. import Image, ImageFile
  17.  
  18. def i32(c):
  19.     return ord(c[3]) + (ord(c[2])<<8) + (ord(c[1])<<16) + (ord(c[0])<<24)
  20.  
  21. def _accept(prefix):
  22.     return i32(prefix) >= 20 and i32(prefix[4:8]) == 1
  23.  
  24. class GbrImageFile(ImageFile.ImageFile):
  25.  
  26.     format = "GBR"
  27.     format_description = "GIMP brush file"
  28.  
  29.     def _open(self):
  30.  
  31.     header_size = i32(self.fp.read(4))
  32.     version = i32(self.fp.read(4))
  33.     if header_size < 20 or version != 1:
  34.         raise SyntaxError, "not a GIMP brush"
  35.  
  36.     width = i32(self.fp.read(4))
  37.     height = i32(self.fp.read(4))
  38.     bytes = i32(self.fp.read(4))
  39.     if width <= 0 or height <= 0 or bytes != 1:
  40.         raise SyntaxError, "not a GIMP brush"
  41.  
  42.     comment = self.fp.read(header_size - 20)[:-1]
  43.  
  44.     self.mode = "L"
  45.     self.size = width, height
  46.  
  47.     self.info["comment"] = comment
  48.  
  49.     # Since the brush is so small, we read the data immediately
  50.     self.data = self.fp.read(width * height)
  51.  
  52.     def load(self, modify=0):
  53.  
  54.     if not self.data:
  55.         return
  56.  
  57.     # create an image out of the brush data block
  58.     self.im = Image.core.new(self.mode, self.size)
  59.     self.im.fromstring(self.data)
  60.     self.data = ""
  61.  
  62. #
  63. # registry
  64.  
  65. Image.register_open("GBR", GbrImageFile, _accept)
  66.  
  67. Image.register_extension("GBR", ".gbr")
  68.